<?php
namespace App\Services\Admin;

use Illuminate\Support\Facades\DB;

class VillageService extends BaseService
{
    /**
     * 小区
     */

    //选项
    public function getOption()
    {
        $arys = [];
        $arys['area'] = $this->areaList();
        return $arys;
    }

    /**
     * 视频列表
     * @param int $scenceId 场景ID
     * @param array $pages【start起始页，limit每页记录数】
     * return array
     */
    public function getIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbVillage)->where('del', 1);
        $total = $db->count();
        $lists = $db->skip($start)->take($pages['limit'])->get();
        if (!count($lists)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($lists as $v) {
            $v = (array) $v;
            $areaOne = (array) $this->dbConn($this->dbArea)
                ->where('id', $v['area_id'])
                ->first();
            $area_name = empty($areaOne) ? '-' : $areaOne['title'];
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'street' => $v['street'],
                'area_id' => $v['area_id'],
                'area_name' => $area_name,
                'map' => $v['map'],
                'uptime' => $v['uptime'],
            ];
        }
        if (!count($arys)) {
            return $this->toback(-2, '没有了');
        }
        return $this->toback(0, '', $arys, $total);
    }

    //详情
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbVillage)
            ->where('del', 1)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        //地区
        $area01 = $res['area_id'];
        $area02 = $area03 = 0;;
        $area01_name = $area02_name = $area03_name = '';
        $areaOne = (array) $this->dbConn($this->dbArea)
            ->where('id', $area01)
            ->first();
        if (!empty($areaOne) && $areaOne['pid']) {
            $area02 = $areaOne['pid'];
            $area01_name = $areaOne['title'];
            $cityOne = (array) $this->dbConn($this->dbArea)
                ->where('id', $area02)
                ->first();
        }
        if (!empty($cityOne) && $cityOne['pid']) {
            $area03 = $cityOne['pid'];
            $area02_name = $cityOne['title'];
            $provinceOne = (array) $this->dbConn($this->dbArea)
                ->where('id', $area03)
                ->first();
            $area03_name = empty($provinceOne) ? 0 : $provinceOne['title'];
        }
        $res['area01'] = $area01;
        $res['area02'] = $area02;
        $res['area03'] = $area03;
        $res['area_str_name'] = $area03_name.'，'.$area02_name.'，'.$area01_name;
        //经纬度
        $res['lng'] = $res['lat'] = '';
        if ($res['map']) {
            $maps = explode(',', $res['map']);
            $res['lng'] = $maps[1];
            $res['lat'] = $maps[0];
        }
        return $this->toback(0, '', $res);
    }

    /**
     * 新增
     * @param array $pas
     */
    public function add($pas)
    {
        $pas['uptime'] = $pas['addtime'] = toDate();
        $lastid = $this->dbConn($this->dbVillage)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
//        DB::beginTransaction(); //开启事务
//        try {
//            DB::commit(); //提交事务
//        } catch (\Exception $e) {
//            DB::rollBack(); //事务回滚
//            echo $e->getMessage();
//            return array('code' => -1, 'msg' => 'error');
//        }
    }

    /**
     * 修改
     * @param int $id
     * @param array $pas
     */
    public function modify($id, $pas)
    {
        //场景有没有
        $scenceOne = (array) $this->dbConn($this->dbVillage)
            ->where('id', $id)
            ->first();
        if (empty($scenceOne)) {
            return $this->toback(-2, '没有记录');
        }
        $pas['uptime'] = toDate();
        $res = $this->dbConn($this->dbVillage)->where('id', $id)->update($pas);
        if (!$res) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 删除
     * @param int $vid 视频ID
     * return array
     */
    public function del($id)
    {
        $res = (array) $this->dbConn($this->dbVillage)
            ->where('del', 1)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        //视频
        $up = $this->dbConn($this->dbVillage)->where('id', $id)
            ->update(['del' => 2, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
//        DB::beginTransaction(); //开启事务
//        try {
//            DB::commit(); //提交事务
//            return array('code' => 0, 'msg' => 'success');
//        } catch (\Exception $e) {
//            DB::rollBack(); //事务回滚
//            echo $e->getMessage();
//            return array('code' => -1, 'msg' => 'error');
//        }
    }
}